package com.easy.work.function.mall.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.base.syslib.Logs;
import com.base.syslib.impl.OnSelectCityImpl;
import com.base.syslib.util.MessageTools;
import com.base.syslib.util.TextUtil;
import com.easy.work.R;
import com.easy.work.app.AppSession;
import com.easy.work.app.BaseActivity;
import com.easy.work.function.mall.bean.AddressBean;
import com.easy.work.function.mall.bean.SaveAddress;
import com.easy.work.http.BaseResponse;
import com.easy.work.http.HttpClient;
import com.easy.work.http.request.SaveAddressRequest;
import com.easy.work.view.dialog.CityDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zhangfeng
 * @date 2017/12/25
 * @desc 编辑地址
 */

public class EditAddressActivity extends BaseActivity {

    private static final String TAG = EditAddressActivity.class.getSimpleName();
    private static final String BEAN = "BEAN";

    public static void start(Context ctx, AddressBean.ListEntity entity) {
        Intent intent = new Intent(ctx, EditAddressActivity.class);
        intent.putExtra(BEAN, entity);
        ctx.startActivity(intent);
    }

    private AddressBean.ListEntity entity;

    @BindView(R.id.tv_area)
    TextView mTvArea;
    @BindView(R.id.et_receive_name)
    EditText mEtReceiveName;
    @BindView(R.id.et_receive_phone)
    EditText mEtReceivePhone;
    @BindView(R.id.et_address_detail)
    EditText mEtAddressDetail;
    @BindView(R.id.checkBox)
    CheckBox checkBox;

    private String province;
    private String city;
    private String area;
    private String modifier = "";
    private String consigneeId = "";
    private boolean isCheck;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_address);
        ButterKnife.bind(this);
        initTitle("编辑地址", "保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveAddress();
            }
        });
        entity = (AddressBean.ListEntity) getIntent().getSerializableExtra(BEAN);
        if (entity != null) {
            mEtReceiveName.setText(entity.name);
            mEtReceivePhone.setText(entity.phone);
            mEtAddressDetail.setText(entity.address);
            modifier = entity.modifier;
            mTvArea.setText(entity.provinceText + entity.cityText + entity.areaText);
            consigneeId = entity.consigneeId;
            isCheck = entity.isDefault == 1;
            checkBox.setChecked(isCheck);
        }


        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isCheck = isChecked;
            }
        });
    }


    /**
     * 获取所有收货地址
     */
    private void saveAddress() {
        String receiveName = mEtReceiveName.getText().toString();
        if (TextUtils.isEmpty(receiveName)) {
            MessageTools.showToast(getApplicationContext(), "请输入收货人姓名");
            return;
        }
        String receivePhone = mEtReceivePhone.getText().toString();
        if (TextUtils.isEmpty(receivePhone)) {
            MessageTools.showToast(getApplicationContext(), "请输入联系电话");
            return;
        }
        if (!TextUtil.isMobile(receivePhone)) {
            MessageTools.showToast(getApplicationContext(), "请输入正确的联系电话");
            return;
        }
        String address = mTvArea.getText().toString();
        if (TextUtils.isEmpty(address)) {
            MessageTools.showToast(getApplicationContext(), "请选择地区");
            return;
        }
        String detail = mEtAddressDetail.getText().toString();
        if (TextUtils.isEmpty(detail)) {
            MessageTools.showToast(getApplicationContext(), "请选择您的详细地址");
            return;
        }
        showDialog();
        final SaveAddressRequest request = new SaveAddressRequest();
        request.consigneeId = consigneeId;
        request.userId = AppSession.getInstance().getUserId();
        request.name = receiveName;
        request.phone = receivePhone;
        request.address = detail;
        request.province = province;
        request.city = city;
        request.area = area;
        request.modifier = modifier;
        request.isDefault = isCheck ? 1 : 0;
        Logs.e(TAG, "name:" + request.name);
        Logs.e(TAG, "phone:" + request.phone);
        Logs.e(TAG, "address:" + detail);
        Logs.e(TAG, "province:" + province);
        Logs.e(TAG, "city:" + city);
        Logs.e(TAG, "area:" + area);
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                closeDialog();
                handleData(response);
            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                closeDialog();
                MessageTools.showToast(getApplicationContext(), msg);
            }
        });
    }

    /**
     * 数据处理
     */
    private void handleData(BaseResponse response) {
        SaveAddress bean = (SaveAddress) response;
        Logs.e(TAG, "save success");
        finish();
    }

    @OnClick(R.id.tv_area)
    void selectArea() {
        new CityDialog(this).builder().setPositiveButton("确定", new OnSelectCityImpl() {
            @Override
            public void selectCity(String address, String provinceCode, String cityCode, String areaCode) {
                mTvArea.setText(address);
                province = provinceCode;
                city = cityCode;
                area = areaCode;
            }
        }).setNegativeButton("取消").show();
    }
}
